/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System.Linq;


namespace CrSyncFilesUtils
{

    /// <summary>
    /// 
    /// Class for handling and parsing command line arguments. The argument structure is based on Unix.
    /// 
    /// NOTE: We copied SyncToy's command line documentation and modified a bit so that the commands are more self documenting.
    /// 
    /// CRSyncFile can be run from the command line to provide support for scheduling or automating folder pair execution. The command line executable by default is located at: [System Drive]\Program Files\SyncToy 2.1\SyncToyCmd.exe. 
    /// SyncToy does not support running multiple copies of SyncToyCmd.exe at the same time. If a second instance of SyncToyCmd.exe is executed, there will be no errors and the command will not be executed.
    ///
    /// The parameters provided by this command line interface are described below: 
    ///
    /// Run all folder pairs that are marked as Active For Run All. 
    ///     -R | --Run
    /// 
    /// Run the named folder pair (only one folder pair can be specified). 
    ///     -r |  --run [folder pair]
    /// 
    /// Sample Usage:
    ///      
    ///     CrSyncFiles.exe -r MyFolderPair
    /// 
    ///     CrSyncFiles.exe -R 
    /// 
    /// NOTE: You can also run folder pair(s) in preview mode by adding "-p | --preview"  to the end:
    /// 
    ///     CrSyncFiles.exe -r MyFolderPair -p
    /// 
    ///     CrSyncFiles.exe -R -p
    ///  
    /// NOTE: A space is required for each paramater.
    ///
    /// It is also possible to manage folder pairs from the command line. 
    /// 
    /// CrSyncFiles does not support running multiple copies of CrSyncFiles.exe at the same time. 
    /// If CrSyncFiles is already running in in the Task Bar, and a second instance of CrSyncFiles.exe is launched with or without any command line parameters, it will simply bring the already running instance into focus and no errors will be reported. 
    ///
    /// The parameters associated with CrSyncFiles.exe are described below:
    /// 
    /// 
    /// List available folder pairs:
    /// 
    ///     -l | --list
    /// 
    /// Sample Usage:
    /// 
    ///     CrSyncFiles.exe -l
    ///  
    ///
    /// Creates a new folder pair with the specified mandatory parameters:
    /// 
    ///     -c | --create  [name] --left [path] --right [path]  [--echo | --synchronize | --contribute ]
    /// 
    /// Sample Usage:
    /// 
    ///     CrSyncFiles.exe -c "source to destination" --left "C:\tmp\source" --right "C:\tmp\destination" --synchronize
    /// 
    /// Optional parameters:
    /// 
    ///     --check | --check-file-contents  Check file contents during synchronization ( uses hash value for each matched file contents )
    ///     
    ///     --exclude [filter] specifies exclude filter for files
    /// 
    ///     --include [filter] specifies include filter for files
    /// 
    /// Sample Usage:
    /// 
    ///     CrSyncFiles.exe -c "source to destination" --left "C:\tmp\source" --right "C:\tmp\destination"  --exclude "*.jpg,*.png" --include "*.doc,*.xls" --check  --synchronize 
    /// 
    /// 
    /// Delete a folder pair with the specified folder pair name:
    /// 
    ///     -d | --delete
    /// 
    /// Sample Usage:
    /// 
    ///     CrSyncFiles.exe -d "source to destination"
    /// 
    /// 
    /// Other parameters:
    ///  
    ///     -? | -h | --help Display Help 
    ///  
    /// 
    /// 
    /// Addition command line parameters:
    /// 
    /// --crsyncfiles           : this parameter tells CRSyncFiles to use the ConnectionRoad command line structure ( as opposed to SyncToy )
    /// --connectionroad        : this parameter tells CRSyncFiles to use the ConnectionRoad command line structure ( as opposed to SyncToy )
    /// ~                       : this parameter tells CRSyncFiles to use the ConnectionRoad command line structure ( as opposed to SyncToy )
    /// --cr                    : this parameter tells CRSyncFiles to use the ConnectionRoad command line structure ( as opposed to SyncToy )
    /// --connectionroad        : this parameter tells CRSyncFiles to use the ConnectionRoad command line structure ( as opposed to SyncToy )
    /// -x                      : this parameter tells CRSyncFiles to use the ConnectionRoad command line structure ( as opposed to SyncToy )
    /// --console               : this parameter tells CRSyncFiles to use the ConnectionRoad command line structure ( as opposed to SyncToy )
    /// 
    /// These are used to signify CRSyncFiles 
    /// 
    /// 
    /// </summary>
    public class CrSyncFileCmdParser
    {
        public CrSyncFileCmdParser()
        {
            
        }

        public CrSyncFileCmdParser(string[] arguments)
        {
            Arguments = arguments;
        }


        private int CurrentIndex { get; set; }

        public string[] Arguments { get; set; }


        /// <summary>
        /// The resulting argument or the user input.
        /// </summary>
        public string CurrentArgument { get; set; }

        public bool ContainsParameters(string[] parameters,bool acceptsInput)
        {
            if ((parameters != null) && (parameters.Length >= 1))
            {
                var arguments = Arguments.ToList();

                foreach (var parameter in parameters)
                {
                    if (arguments.Contains(parameter))
                    {
                        if (acceptsInput)
                        {
                            CurrentIndex = arguments.IndexOf(parameter) + 1;
                            CurrentArgument = arguments[CurrentIndex];
                        }
                        return true;
                    }
                }
                return false;
            }

            return false;
        }

        /// <summary>
        /// Move to the next arguments
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public string Next(int? index)
        {
            if(index == null)
            {
                CurrentIndex++;
                CurrentArgument = Arguments[CurrentIndex];
                return CurrentArgument;
            }
            CurrentIndex = (int)index;
            CurrentArgument = Arguments[CurrentIndex];
            return CurrentArgument;
        }
    }
}
